package com.market.sys.util;/*
 *文件名: TimeUtil
 *创建时间:2022/12/5 16:06
 *version: 1.0
 *since: 1.0
 */

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Date;

import static com.market.sys.util.Constants.TIME_LENGTH_NOT_SECONDS;
import static com.market.sys.util.Constants.TIME_LENGTH_SECONDS;


/**
 * @author 苑松硕
 */
public class TimeUtil {
    private static String time;

    public static String timeYmd(Date date) {
        //年月日
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        time = sdf.format(date);
        return time;
    }

    public static String timeYmdhm(Date date) {
        //年月日，时分
        SimpleDateFormat sdf2 = new SimpleDateFormat("yyyy-MM-dd HH:mm");
        time = sdf2.format(date);
        return time;
    }

    public static String timeYmdhms(Date date) {
        //年月日，时分秒
        SimpleDateFormat sdf3 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        time = sdf3.format(date);
        return time;
    }

    /**
     * convertStringToDate与getTimeStamp方法的结合
     *
     * @param time 字符串日期
     * @return 完美时间
     */
    public static String completeGetTimeStamp(String time) {
        if (time == null) {
            return "暂无时间";
        }
        LocalDateTime localDateTime = null;
        if (time.length() == TIME_LENGTH_SECONDS) {
            //时分秒
            time = time.substring(0, TIME_LENGTH_SECONDS);
            localDateTime = convertStringToDate(time);
        }
        if (time.length() == TIME_LENGTH_NOT_SECONDS) {
            //时分
            time = time.substring(0, TIME_LENGTH_NOT_SECONDS);
            localDateTime = convertStringToDateNotSeconds(time);
        }
        assert localDateTime != null;
        return getTimeStamp(localDateTime);
    }

    /**
     * 将字符串转换为日期，格式为：yyyy-MM-dd HH:mm
     *
     * @param time 字符串日期
     * @return LocalDateTime
     */
    private static LocalDateTime convertStringToDateNotSeconds(String time) {
        DateTimeFormatter dft = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm");
        return LocalDateTime.parse(time, dft);
    }

    /**
     * 将字符串转换为日期，格式为：yyyy-MM-dd HH:mm:ss
     *
     * @param time 字符串日期
     * @return LocalDateTime
     */
    private static LocalDateTime convertStringToDate(String time) {
        DateTimeFormatter dft = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        return LocalDateTime.parse(time, dft);
    }

    //根据逻辑，将时间转换为 xx天前，或者是某年某月某日等
    private static String getTimeStamp(LocalDateTime localDateTime) {
        String returnDate = null;

        int currentYear = LocalDateTime.now().getYear();
        int valueYear = localDateTime.getYear();

        if (currentYear > valueYear) {
            return localDateTime.getYear() + "-" + changeMonth(localDateTime) + "-" + (localDateTime.getDayOfMonth() < 10 ? "0" + localDateTime.getDayOfMonth() : localDateTime.getDayOfMonth());
        }

        int currentMonth = LocalDateTime.now().getMonth().getValue();
        int valueMonth = localDateTime.getMonth().getValue();

        if (currentMonth > valueMonth) {
            String theMonth = changeMonth(localDateTime);
            if (localDateTime.getDayOfMonth() > 10) {
                return theMonth + "-" + localDateTime.getDayOfMonth();
            } else {
                return theMonth + "-0" + localDateTime.getDayOfMonth();
            }
        }

        int today = LocalDateTime.now().getDayOfMonth();
        int theDay = localDateTime.getDayOfMonth();

        int betweenDay = today - theDay;

        if (betweenDay < 4 && betweenDay >= 1) {
            for (int i = 0; i < 4; i++) {
                if (betweenDay == i) {
                    returnDate = i + "天前";
                }
            }
        } else if (betweenDay >= 4) {
            String dayOfMonthStr = localDateTime.getDayOfMonth() < 10 ? "0" + localDateTime.getDayOfMonth() : String.valueOf(localDateTime.getDayOfMonth());
            int nowYear = LocalDateTime.now().getYear();
            int timeYear = localDateTime.getYear();
            String theMonth = changeMonth(localDateTime);
            if (timeYear < nowYear) {
                returnDate = timeYear + "-" + theMonth + "-" + dayOfMonthStr;
            } else {
                returnDate = theMonth + "-" + dayOfMonthStr;
            }
        } else if (betweenDay == 0) {
            String hourStr = localDateTime.getHour() < 10 ? "0" + localDateTime.getHour() : String.valueOf(localDateTime.getHour());
            String minuteStr = localDateTime.getMinute() < 10 ? "0" + localDateTime.getMinute() : String.valueOf(localDateTime.getMinute());
            returnDate = hourStr + ":" + minuteStr;
        }

        return returnDate;
    }


    private static String changeMonth(LocalDateTime month) {
        String stringMonth = month.getMonth().toString();
        return switch (stringMonth) {
            case "JANUARY" -> "01";
            case "FEBRUARY" -> "02";
            case "MARCH" -> "03";
            case "APRIL" -> "04";
            case "MAY" -> "05";
            case "JUNE" -> "06";
            case "JULY" -> "07";
            case "AUGUST" -> "08";
            case "SEPTEMBER" -> "09";
            case "OCTOBER" -> "10";
            case "NOVEMBER" -> "11";
            case "December" -> "12";
            default -> null;
        };
    }


    //当前现在的时间是否比传入的时间参数大
    public static boolean isCurrentTimeAfterTime(String time) {
        boolean flag = false;
        if (time.length() == 16) {
            DateTimeFormatter df = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm");
            LocalDateTime localDateTime = LocalDateTime.parse(time, df);
            flag = localDateTime.isBefore(LocalDateTime.now());
        } else if (time.length() == 19) {
            DateTimeFormatter df = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
            LocalDateTime localDateTime = LocalDateTime.parse(time, df);
            flag = localDateTime.isBefore(LocalDateTime.now());
        } else if (time.length() == 13) {
            DateTimeFormatter df = DateTimeFormatter.ofPattern("yyyy-MM-dd HH");
            LocalDateTime localDateTime = LocalDateTime.parse(time, df);
            flag = localDateTime.isBefore(LocalDateTime.now());
        } else if (time.length() == 10) {
            DateTimeFormatter df = DateTimeFormatter.ofPattern("yyyy-MM-dd");
            LocalDateTime localDateTime = LocalDateTime.parse(time, df);
            flag = localDateTime.isBefore(LocalDateTime.now());
        } else if (time.length() == 7) {
            DateTimeFormatter df = DateTimeFormatter.ofPattern("yyyy-MM");
            LocalDateTime localDateTime = LocalDateTime.parse(time, df);
            flag = localDateTime.isBefore(LocalDateTime.now());
        } else if (time.length() == 4) {
            DateTimeFormatter df = DateTimeFormatter.ofPattern("yyyy");
            LocalDateTime localDateTime = LocalDateTime.parse(time, df);
            flag = localDateTime.isBefore(LocalDateTime.now());
        }
        return flag;
    }

    /**
     * 判断时间是否是今天
     */
    public static boolean isTimeToday(String time) {

        boolean flag = false;

        DateTimeFormatter dtf;

        if (time.length() == TIME_LENGTH_SECONDS) {
            dtf = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        } else {
            dtf = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm");
        }

        LocalDateTime localTime = LocalDateTime.parse(time, dtf);

        LocalDateTime startTime = LocalDate.now().atTime(0, 0, 0);

        LocalDateTime endTime = LocalDate.now().atTime(23, 59, 59);

        //如果大于今天的开始日期，小于今天的结束日期
        if (localTime.isAfter(startTime) && localTime.isBefore(endTime)) {
            flag = true;
            System.out.println("时间是今天");
        }
        return flag;
    }

    /**
     * 字符串转时间戳
     */
    public static long stringToTimestamp(String time) {
        String pattern = "yyyy-MM-dd HH:mm:ss";
        long timestamp = 0L;

        SimpleDateFormat sdf = new SimpleDateFormat(pattern);
        try {
            Date date = sdf.parse(time);
            timestamp = date.getTime();
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return timestamp;
    }
}
